# -*- coding: utf-8 -*-
#----------------------------------------------
# @note: system link for running project by timer
# @author: Andrey Simine/Андрей Симин
# @copyright: (с) 2016 Андрей Симин
#----------------------------------------------
__author__ = 'asimine'

from chainlib.basic_link import BasicLink
import time, sys
from pprint import pprint
import requests


class UserLink(BasicLink):

    def __init__(self, context):
        super(UserLink, self).__init__(context)

    def execute(self):
        params = self.context["linkdata"]["parameters"]         

        t = time.localtime()
        current_time = time.strftime("%H:%M:%S", t)
        print(f"current time: {current_time}")

        print(f"Run project by time...")
        print("Context:")
        pprint(self.context)

        res = requests.put(f"http://carousel_restapi:5001/api/1.0/build/create/{self.context['project_id']}")
    
        res.raise_for_status()

        pprint("Running build result: ") 
        pprint(res.json())

        print(f"Run project by time finished successfully.")
